package com.zxl.grocery.microservice.auth.server.util.user;

import com.zxl.grocery.microservice.common.util.jwt.IJWTInfo;
import com.zxl.grocery.microservice.common.util.jwt.JWTHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @Author: zxl
 * @Description: 用户和服务之间的token工具
 * @Date: Created in 20:18 2017/10/14
 * @Modified By:
 */
@Component
public class JwtTokenUtil {

    @Value("${jwt.expire}")
    private int expire;//有效时间
    @Value("${jwt.pri-key.path}")
    private String priKeyPath;//秘钥路径
    @Value("${jwt.pub-key.path}")
    private String pubKeyPath;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * @Author: Lzx
     * @Description: 生成token
     * @param jwtInfo 放入jwt中的信息
     * @Date: 10:13 2017/10/19
     */
    public String generateToken(IJWTInfo jwtInfo) throws Exception {
        return JWTHelper.generateToken(jwtInfo,priKeyPath,expire);
    }

    /**
     * @Author: Lzx
     * @Description: 获取token中的存放的信息
     * @Params:  * @param null
     * @Date: 10:31 2017/10/19
     */
    public IJWTInfo getInfoFromToken(String token) throws Exception {
        return JWTHelper.getInfoFromToken(token,pubKeyPath);
    }


}
